package com.goodlearning.gloriamobile.activity;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.goodlearning.gloriamobile.R;
import com.goodlearning.gloriamobile.commons.Constants;
import com.goodlearning.gloriamobile.commons.Utils;
import com.goodlearning.gloriamobile.net.JsonRequestThread.INetSuccessCallback;
import com.goodlearning.gloriamobile.net.JsonServiceRequester;
import com.goodlearning.gloriamobile.net.JsonWrapper;
/**
 * Login dialog 를 대체할 LoginActivity 
 * @author rapty54
 *
 */
public class LoginActivity extends Activity {
	
	private EditText idEt,pwEt;
	
	private Button loginBtn,searchingIdPw;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.base_dialog_layout);
		
		Utils.log("FIRST_INIT_LOGIN_STATE:"+"["+Utils.getStringByPreference(LoginActivity.this, Constants.PRE_STRING_LOGIN_SUCCESS, "" )+"]");
		
		if(Utils.getStringByPreference(LoginActivity.this, Constants.PRE_STRING_LOGIN_SUCCESS, "").equals("")){
			setButton();
		}else{
			Intent i = new Intent(LoginActivity.this, Plan.class ).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
			finish();
		}
	}
	
	/**
	 * 모두 여기서 다한다.전부 다다
	 * 로그인에 대한 성공과 실패 여부를 직접 설정하고 변경할 수있다.
	 */
	public void setButton(){
		searchingIdPw = (Button) findViewById(R.id.dialog_search_Id_pw);
		idEt = (EditText) findViewById(R.id.dialog_login_email);
		pwEt = (EditText) findViewById(R.id.dialog_login_password);
		loginBtn = (Button) findViewById(R.id.dialog_login_bt);
		
		loginBtn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				JsonServiceRequester.requestLogin(LoginActivity.this, idEt.getText().toString(), pwEt.getText().toString(), new INetSuccessCallback() {
					
					@Override
					public void sendCallback(JsonWrapper jsonWrapper) {
						JSONArray jsonArray= jsonWrapper.getRootArray();
						
						if("0".equals( ((JSONObject)jsonArray.get(0)).get("RESULT").toString() ) ){
							
							Utils.setPreference(LoginActivity.this, Constants.PRE_STRING_USER_ID, idEt.getText().toString().replaceAll(" ", "") );
							Utils.setPreference(LoginActivity.this, Constants.PRE_STRING_USER_PW, pwEt.getText().toString().replaceAll(" ", "") );
							Utils.setPreference(LoginActivity.this, Constants.PRE_STRING_LOGIN_SUCCESS, "LOGIN_SUCCESS");
							
							Intent i = new Intent(LoginActivity.this, Plan.class ).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
							startActivity(i);
							finish();
							
						}else{
							Toast.makeText(LoginActivity.this, "로그인 정보가 옳지 않습니다. 다시 입력해 주세요.", Toast.LENGTH_SHORT).show();
							Utils.setPreference(LoginActivity.this, Constants.PRE_STRING_LOGIN_FAIL, "LOGIN_FAIL");
						}
					}
				});
			}
		});
		/**
		 * 계정 분실시 어떤 Action
		 */
		searchingIdPw.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						Utils.intentCallCustom(LoginActivity.this.getApplicationContext(), Intent.ACTION_CALL, Uri.parse("tel://029448665"));
					}
				}).start();
			}
		});
		
		View loginMainBody=(View)findViewById(R.id.include_login_dialog_body);
		View LeftButton=(View)findViewById(R.id.dialog_title_left_button);
		TextView titleText=(TextView)findViewById(R.id.dialog_title_TextView);
		
		titleText.setText("로그인");
		LeftButton.setVisibility(View.INVISIBLE);
		loginMainBody.setVisibility(View.VISIBLE);	
		
		
	}
	
}
